package com.cos.hospital.ui.other;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cos.hospital.R;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.bean.ImageBean;
import com.cos.hospital.ui.other.adapter.PictureBottomAdapter;
import com.cos.hospital.ui.other.adapter.PictureTopAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class YinShiTuPianActivity extends BaseActivity {

    @BindView(R.id.list_top)
    ViewPager topView;
    @BindView(R.id.list_bottom)
    RecyclerView bottomView;
    PictureTopAdapter topAdapter;
    PictureBottomAdapter bottomAdapter;
    ArrayList<String> pics;
    int position;

    public static void showTuPian(Context context, ArrayList<String> pics, int position) {
        Intent intent = new Intent(context, YinShiTuPianActivity.class);
        intent.putStringArrayListExtra("tupian", pics);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }

    public static void showTuPian(Context context, String[] pics) {
        showTuPian(context, pics, 0);
    }

    public static void showTuPian(Context context, String[] pics, int position) {
        ArrayList<String> tu = new ArrayList<>();
        if (pics != null) {
            for (String a : pics) {
                if (TextUtils.isEmpty(a)) {
                    tu.add("");
                } else {
                    tu.add(a);
                }
            }
        } else {
            return;
        }
        showTuPian(context, tu, position);
    }


    public static void showTu(Context context,List<String> pics, int position) {
        ArrayList<String> tu = new ArrayList<>();
        if (pics != null) {
            for (String a : pics) {
                if (TextUtils.isDigitsOnly(a)) {
                    tu.add("");
                } else {
                    tu.add(a);
                }
            }
        } else {
            return;
        }
        showTuPian(context, tu, position);
    }


    public static void showTuPian(Context context, List<ImageBean> pics, int position) {
        ArrayList<String> list = new ArrayList<>();
        for (ImageBean imageBean : pics) {
            if (imageBean != null) {
                if (TextUtils.isEmpty(imageBean.getFldImgPath())) {
                    list.add("");
                } else {
                    list.add(imageBean.getFldImgPath());
                }
            } else {
                list.add("");
            }
        }
        showTuPian(context, list, position);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        pics = getIntent().getStringArrayListExtra("tupian");
        position = getIntent().getIntExtra("position", 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int layout() {
        return R.layout.activity_yin_shi_tu_pian;
    }

    @Override
    protected void init() {
        if (pics == null || pics.size() == 0) {
            return;
        }
        //设置布局管理器
        LinearLayoutManager topLayoutManager = new LinearLayoutManager(this);
        topLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        LinearLayoutManager bottomLayoutManager = new LinearLayoutManager(this);
        bottomLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        List<Fragment> list = new ArrayList<>();
        for (int i = 0; i < pics.size(); i++) {
            String url = pics.get(i);
            if (TextUtils.isEmpty(url)) {
                if (position >= i && position > 0) {
                    position--;
                }
            } else {
                list.add(TuPianItemFragment.newInstance(url));
            }
        }

        topAdapter = new PictureTopAdapter(getSupportFragmentManager(), list);
        topView.setAdapter(topAdapter);
        bottomAdapter = new PictureBottomAdapter();
        bottomView.setLayoutManager(bottomLayoutManager);
        bottomView.setAdapter(bottomAdapter);
        bottomAdapter.addData(pics);


        bottomAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                topView.setCurrentItem(position);
            }
        });

        if (position < list.size() && position > 0) {
            topView.setCurrentItem(position);
            bottomView.scrollToPosition(position);
        }
    }

    @Override
    protected void logic() {

    }


    public void goBack(View view) {
        finish();
    }
}
